﻿using System;
using System.Collections.Generic;
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using MyHotel_DTO;
using MyHotel_Common; 

namespace MyHotel_DAO
{
    public class LoaiPhongDAO : DAO
    {
        protected override object createObjectFromDatarow(DataRow row)
        {
            LoaiPhong loaiphong = new LoaiPhong();
            loaiphong.MaLoai = Convert.ToInt32(row["MaLoai"]);
            loaiphong.TenLoai = row["TenLoai"].ToString();
            loaiphong.DonGia = Convert.ToInt32(row["DonGia"]);
            return loaiphong;
        }
        
        public  List<LoaiPhong> getList()
        {
            List<LoaiPhong> listloaiphong = new List<LoaiPhong>();
            string nameProc = "SP_LOAIPHONG_GETLIST";
            DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, null);
            return getListFromDataTable<LoaiPhong>(dt);
        } 
        public bool themLoaiPhong(LoaiPhong loaiphong)
        {
            string nameProc = "them_loaiphong";
            SqlParameter[] para = new SqlParameter[2];
            para[0] = new SqlParameter("@tenloai",loaiphong.TenLoai);
            para[1] = new SqlParameter("@dongia",loaiphong.DonGia); 
            int n = DataProvider.executeStoreProcedureNonQuery(nameProc, para);
            return (n == 1); 
        } 
        public LoaiPhong getLoaiPhongbyMaLoai(int maloai)
        {
            LoaiPhong loaiphong = new LoaiPhong();
            try
            {
                string nameProc = "SP_GETLOAIPHONG_BY_MALOAI";
                SqlParameter[] para = new SqlParameter[1];
                para[0] = new SqlParameter("@MaLoai", maloai);
                DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, para);
                if (dt.Rows.Count != 0)
                    loaiphong = (LoaiPhong)createObjectFromDatarow(dt.Rows[0]); 
            }
            catch (Exception ex)
            {
                throw ex;
            } 
            return loaiphong;
        } 

        public LoaiPhong getLoaiPhongbyTenPhong(string tenphong)
        {
            LoaiPhong loaiphong = new LoaiPhong();
            try
            {
                string nameProc = "loaiphong_by_tenphong";
                SqlParameter[] para = new SqlParameter[1];
                para[0] = new SqlParameter("@tenphong", tenphong);
                DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, para);
                if (dt.Rows.Count != 0)
                    loaiphong = (LoaiPhong)createObjectFromDatarow(dt.Rows[0]); 
            }
            catch (Exception ex)
            {
                throw ex;
            } 
            return loaiphong;
        }

        public int getMaxDonGia()
        {
            int max = 0;
            try
            {
                string nameProc = "SP_GETMAXDONGIA_LOAIPHONG";
                DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, null);
                max = Convert.ToInt32(dt.Rows[0]["dongia"].ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return max;
        }
        public int getMinDonGia()
        {
            int min = 0;
            try
            {
                string nameProc = "SP_GETMINDONGIA_LOAIPHONG";
                DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, null);
                min = Convert.ToInt32(dt.Rows[0]["dongia"].ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return min;
        }
        public bool capNhatLoaiPhong(LoaiPhong loaiphong)
        {
            string nameProc = "capnhat_loaiphong";
            SqlParameter [] para = new SqlParameter[3];
            para[0] = new SqlParameter("@maloai", loaiphong.MaLoai);
            para[1] = new SqlParameter("@tenloai", loaiphong.TenLoai);
            para[2] = new SqlParameter("@dongia", loaiphong.DonGia);
            int n = DataProvider.executeStoreProcedureNonQuery(nameProc, para);
            return (n == 1); 
        }
        public bool xoaLoaiPhong(LoaiPhong loaiphong)
        {
            string nameProc = "xoa_loaiphong";
            SqlParameter para = new SqlParameter("@tenloai", loaiphong.TenLoai);
            int n = DataProvider.executeStoreProcedureNonQuery(nameProc, para);
            return true;
        }
        public bool CheckLoaiPhongByTenLoai(string tenloai)
        {
            string nameProc = "SP_GETLOAIPHONG_BY_TENLOAI";
            SqlParameter para = new SqlParameter("@tenloai", tenloai);
            DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, para);
            
            return (dt.Rows.Count == 1); 
        }
        public bool CheckLoaiPhongByMaLoai(int maloai)
        {
            string nameProc = "SP_GETLOAIPHONG_BY_MALOAI";
            SqlParameter[] para = new SqlParameter[1];
            para[0] = new SqlParameter("@MaLoai", maloai);
            DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, para);
            return (dt.Rows.Count == 1); 
        } 
        public bool CheckPhongByMaLoai(int maloai)
        {
            string nameProc = "kiemtraphong_by_maloaiphong";
            SqlParameter para = new SqlParameter("@maloai", maloai);
            DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, para);
            return (dt.Rows.Count != 0); 
        }
        public bool CheckLoaiPhongByTenLoai_MaLoai(string tenloai, int maloai)
        {
            string nameProc = "SP_GETLOAIPHONG_BY_TENLOAI_MALOAI";
            SqlParameter[] para = new SqlParameter[2];
            para[0] = new SqlParameter("@maloai", maloai);
            para[1] = new SqlParameter("@tenloai", tenloai);
            
            DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, para);
            return (dt.Rows.Count == 1);
        }
    }
}
